<?php
/**
 * 中职管理系统访问
 */

namespace console\spider;


use console\spider\ocr\AipOcr;
use console\spider\utils\TimeUtils;

class ZzglLogin
{
    /**
     * 百度 OCR APPID AK SK
     */
    const APP_ID = '23753903';
    const API_KEY = 'HSur4BOg96IDa1UIQN8LGCsD';
    const SECRET_KEY = 'BZGKQ8H5cq5vdMnhtLiSP0zGpddDBkGi';
    /**
     * ZZXS 登录信息
     */
    const USER_NAME = 'MzAyMzI1Mg==';//用户名
    const TOKEN_NAME = 'token';
    const PASSWORD = '195ef371dc5f5a2f2d88b6cf42b26339';//密码


    /**
     * 登录页面 login page url
     */
    const login_page_url = "http://zzxs.gxeduyun.edu.cn/pages/login.jsp";

    /**
     * 验证码 login url
     */
    const verify_code_url = "http://zzxs.gxeduyun.edu.cn/verifycode.do?width=70&height=20&codecount=4&codestyle=digit&timestamp=";

    /**
     * 登录 login url
     */
    const loginUrl =  "http://zzxs.gxeduyun.edu.cn/Home.action";


    /**
     * @return int|mixed
     */
    public static function getCookie()
    {
        echo "正在获取COOKIE...\n";

        $curl = curl_init();
        $timeout = 5;
        curl_setopt($curl, CURLOPT_URL, self::login_page_url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//获取到的内容作为变量存储，设置为1或者TRUE就不直接输出
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);//链接时间
        curl_setopt($curl,CURLOPT_COOKIEJAR, Params::cookie_file);//获取COOKIE并存储，把获得到的cookie存储为文件
        $contents = curl_exec($curl);
        curl_close($curl);

        if(empty($contents))
        {
            echo "zzgl server is turn off\n";
            return -1;
        }

        preg_match('/ name=\"token\" value=\"(.*)\"/', $contents, $token);

        return $token[1];
    }

    /**
     * @return int|mixed
     */
    public static function getVerifyCode()
    {
        do
        {
            echo "正在取验证码........\n";
            $curl = curl_init();
            $verify_code_url = self::verify_code_url.TimeUtils::gettimestamp();
            curl_setopt($curl, CURLOPT_URL, $verify_code_url);
            curl_setopt($curl, CURLOPT_COOKIEFILE, Params::cookie_file);
            curl_setopt($curl, CURLOPT_HEADER, 0);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $img = curl_exec($curl);
            curl_close($curl);

            //识别验证码
            $client = new AipOcr(self::APP_ID, self::API_KEY, self::SECRET_KEY);
            if(empty($client))
            {
                echo "api ocr create error\n";
                return -1;
            }
            //可选参数
            $options=array();
            //本地文件识别
            $ocr_return = $client->numbers($img,$options);

            if(empty( $ocr_return['words_result']))
            {
                echo "error_code: ".$ocr_return['error_code']."\nerror_msg: ".$ocr_return['error_msg']."\n";
                return -1;
            }

            $verifyCode = $ocr_return['words_result'][0]['words'];
            echo "验证码：$verifyCode\n";
        }while(strlen($verifyCode) != 4);

        return $verifyCode;
    }

    /**
     * 登录
     * @param $token
     * @param $verifyCode
     * @return int
     */
    public static function login($token,$verifyCode)
    {
        echo "login......\n";
        $post = "userName=".self::USER_NAME."&struts.token.name=".self::TOKEN_NAME."&token=".$token."&password=".self::PASSWORD."&verifyCode=$verifyCode";

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, self::loginUrl);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
        curl_setopt($curl, CURLOPT_COOKIEFILE, Params::cookie_file);
        $result=curl_exec($curl);
        curl_close($curl);

        //这一块根据自己抓包获取到的网站上的数据来做判断
        if(substr_count($result,"退出")){
            echo "登录成功\n";
            return 0 ;
        }
        else{
            echo "登录失败\n";
            return -1;
        }
    }


    public static function ZzxsLogin()
    {
        do
        {

            $token = self::getCookie();
            if($token == -1)
                return -1;

            $verifyCode = self::getVerifyCode();
            if($verifyCode == -1 )
            {
                echo "获取验证码失败\n";
                continue;
            }

        }while(self::login($token,$verifyCode) !=0);

        return 0 ;
    }
}